#include "Drive.h"
#include "NtfsParse.h"

Drive::Drive(const std::wstring &_wsVolumeName, const std::wstring &_wsName, const std::wstring &_wsPath)
	: m_wsVolumeName(_wsVolumeName)
	, m_wsName(_wsName)
	, m_wsPath(_wsPath)
{
}

Drive::~Drive()
{
}

int Drive::ReadDrive()
{
	return RET_DEFAULT_CORRECT;
}

int Drive::GetFileList(uint64_t _uFtsNo, std::vector<File*>& _vecFileList)
{
	return RET_DEFAULT_CORRECT;
}

int Drive::UpdateFileList(uint64_t _uFtsParentNo, std::vector<File*>& _vecFileList)
{
	return RET_DEFAULT_CORRECT;
}

void Drive::CopyFileById(CopyFileData *_pCopyData)
{}

int Drive::ReadFileDataById(uint64_t _uFtsNo, char *_pDataBlock, uint32_t _iDataBlockSize)
{
	return RET_DEFAULT_CORRECT;
}

int Drive::BuildDriveIndex()
{
	return RET_DEFAULT_CORRECT;
}

bool Drive::IsFileExist(const std::wstring& _wstrFileName, uint64_t _uPid, uint64_t& _uId, const std::vector<std::wstring>& _vecIds)
{
	return false;
}

NtfsDrive::NtfsDrive(const std::wstring &_wsVolumeName, const std::wstring &_wsName, const std::wstring &_wsPath)
	: Drive(_wsVolumeName, _wsName, _wsPath)
{
	m_pFsParse = new NtfsParse(this);
}

NtfsDrive::~NtfsDrive()
{
	if (m_pFsParse != nullptr)
	{
		delete m_pFsParse;
	}
}

int NtfsDrive::ReadDrive()
{
	return m_pFsParse->ReadDrive(m_wsPath, *this);
}

int NtfsDrive::GetFileList(uint64_t _uFtsParentNo, std::vector<File*>& _vecFileList)
{
	int iRet = RET_DEFAULT_CORRECT;
	if (nullptr == m_pFsParse) return RET_DEFAULT_ERROR;

	return m_pFsParse->ParseFile(_uFtsParentNo, _vecFileList);
}

int NtfsDrive::BuildDriveIndex()
{
	int iRet = RET_DEFAULT_CORRECT;
	if (nullptr == m_pFsParse) return RET_DEFAULT_ERROR;
	m_indexLocker.lockForRead();
	bool blUnBuildIndex = m_mapIndex.empty();
	m_indexLocker.unlock();
	if (blUnBuildIndex)
	{
		return m_pFsParse->BuildDriveIndex();
	}

	return iRet;
}

bool NtfsDrive::IsFileExist(const std::wstring& _wstrFileName, uint64_t _uPid, uint64_t &_uId, const std::vector<std::wstring>& _vecIds)
{
	bool blFind = false;
	if (!_vecIds.empty())
	{
		m_indexLocker.lockForRead();
		for (auto id : _vecIds)
		{
			uint64_t uId = QString::fromStdWString(id).toULongLong();
			if (m_mapIndex.find(uId) != m_mapIndex.end())
			{
				if (m_mapIndex[uId].m_wstrFileName == _wstrFileName)
				{
					_uId = uId;
					blFind = true;
				}
			}
		}

		m_indexLocker.unlock();
	}
	
	return blFind;
}

void NtfsDrive::CopyFileById(CopyFileData *_pCopyData)
{
	if (m_pFsParse == nullptr)
	{
		return;
	}

	m_pFsParse->CopyFileById(_pCopyData);
}

int NtfsDrive::ReadFileDataById(uint64_t _uFtsNo, char *_pDataBlock, uint32_t _iDataBlockSize)
{
	int iRet = RET_DEFAULT_CORRECT;
	return iRet;
}

int NtfsDrive::UpdateFileList(uint64_t _uFtsParentNo, std::vector<File*>& _vecFileList)
{
	int iRet = RET_DEFAULT_CORRECT;
	if (nullptr == m_pFsParse) return RET_DEFAULT_ERROR;

	return m_pFsParse->ReparseFile(_uFtsParentNo, _vecFileList);
}